﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using Microsoft.VSSDK.Tools.VsIdeTesting;
using Microsoft.VisualStudio.Shell.Interop;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Views;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Tests.Integration.Instruments
{
    public class WindowPaneManager<TPackage>
        where TPackage : Package
    {
        public IWindowPaneWrapper Get<TWindowPane>()
            where TWindowPane : ToolWindowPane
        {
            var package = PackageLoader.LoadPackage<TPackage>();
            var windowPane = package.FindWindowPane(typeof(TWindowPane), 0, false) as TWindowPane;
            return (windowPane != null) ? (IWindowPaneWrapper)new WindowPaneWrapper<TWindowPane>(windowPane) : (IWindowPaneWrapper)new NullWindowPane();
        }

        public bool Exists<TWindowPane>()
            where TWindowPane : ToolWindowPane
        {
            var package = PackageLoader.LoadPackage<TPackage>();
            var windowPane = package.FindWindowPane(typeof(TWindowPane), 0, false) as TWindowPane;
            if (windowPane == null)
            {
                return false;
            }

            var frame = (IVsWindowFrame)windowPane.Frame;
            int result;
            frame.IsOnScreen(out result);
            return result == 1;
        }
    }
}
